package binaryTree.re12_04;

import binaryTree.basic.TreeNode;

public class Class112 {

    boolean  ans=false;
    public boolean hasPathSum(TreeNode root, int targetSum) {
    if (root==null){
        return false;
    }
    traverse(root,targetSum);
    return  ans;

    }

    public  void   traverse(TreeNode root,int targetSum){
        if (root==null){
            return;
        }
        //TODO:2023/12/4 先计算然后再判断
        targetSum-= root.val;
        if (root.left==null&&root.right==null&&targetSum==0){
            ans=true;
            return;
        }
        //TODO:2023/12/4 放在这个位置是不行的
//        targetSum-= root.val;

        traverse(root.left,targetSum);
        traverse(root.right,targetSum);
        targetSum+=root.val;

    }

    public static void main(String[] args) {
        TreeNode treeNode1 = new TreeNode(5);
        TreeNode treeNode2 = new TreeNode(4);
        TreeNode treeNode3 = new TreeNode(8);
        TreeNode treeNode4 = new TreeNode(11);
        TreeNode treeNode5 = new TreeNode(13);
        TreeNode treeNode6 = new TreeNode(4);
        TreeNode treeNode7 = new TreeNode(7);
        TreeNode treeNode8 = new TreeNode(2);
        TreeNode treeNode9 = new TreeNode(1);
        treeNode1.left=treeNode2;
        treeNode1.right=treeNode3;
        treeNode2.left=treeNode4;
        treeNode3.left=treeNode5;
        treeNode3.right=treeNode6;
        treeNode4.left=treeNode7;
        treeNode4.right=treeNode8;
        treeNode6.right=treeNode9;
        Class112 class112 = new Class112();
        System.out.println(class112.hasPathSum(treeNode1, 22));


    }


}
